package com.simoniu.hbase.phoenixmybatis.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.simoniu.hbase.phoenixmybatis.entity.Students;
import com.simoniu.hbase.phoenixmybatis.service.StudentsService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping({"students"})
public class StudentsController {

    @Resource
    private StudentsService studentsService;

    @GetMapping({"list"})
    public Map<String, Object> list() {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            List<Students> studentsList = studentsService.selectAll();
            result.put("code", 200);
            result.put("msg", "学生资料查询成功！");
            result.put("data", studentsList);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            result.put("code", 500);
            result.put("msg", "程序出现异常！");
            return result;
        }
    }

    @GetMapping({"page"})
    public Map<String, Object> page(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            long start = System.currentTimeMillis();
            IPage<Students> ipage = studentsService.page(page, size);
            System.out.println("分页查询耗时 =" + (System.currentTimeMillis() - start) + "毫秒");
            result.put("code", 200);
            result.put("msg", "学生资料分页查询成功！");
            result.put("data", ipage);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            ex.printStackTrace();
            result.put("code", 500);
            result.put("msg", "程序出现异常！");
            return result;
        }

    }

    @PostMapping({"save"})
    public Map<String, Object> save(Students s) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (studentsService.saveStudent(s)) {
                result.put("code", 200);
                result.put("msg", "保存学习资料成功！");

            } else {
                result.put("code", 400);
                result.put("msg", "保存学习资料失败！");
            }
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            ex.printStackTrace();
            ex.printStackTrace();
            result.put("code", 500);
            result.put("msg", "程序出现异常！");
            return result;
        }

    }
}
